const EMAIL_REGEX = /^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$/

const PHONE_REGEX = /^1\d{10}$/

export const validateEmail = function (str) {
  return EMAIL_REGEX.test(str)
}

export const validatePhone = function (str) {
  return PHONE_REGEX.test(str)
}

export const validatePasswordStrictly = function (str) {
  return (
    !!str &&
    str.length >= 8 &&
    /[a-zA-Z]+/.test(str) &&
    /\d+/.test(str) &&
    /[^a-zA-Z0-9]+/.test(str)
  )
}

// 表单验证规则
export const requiredRule = (fieldName) => ({
  required: true,
  message: `${fieldName}不能为空`,
  trigger: ['blur'],
})

export const strictPasswordRule = () => ({
  required: true,
  trigger: ['blur', 'input'],
  validator: (rule, value) => validatePasswordStrictly(value.trim()),
  message: '请输入至少8位字符，包含数字、字母和其他特殊字符',
})

export const confirmPasswordRule = (formValue, field) => ({
  required: true,
  trigger: ['blur', 'input'],
  validator: (rule, value) => value.trim() === formValue.value[field].trim(),
  message: '两次密码输入不一致',
})

export const emailRule = (required = true) => ({
  trigger: ['blur', 'change'],
  validator: (rule, value) =>
    required ? validateEmail(value) : !value.trim() || validateEmail(value),
  message: '请输入有效的邮箱',
})
